﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Xml;

namespace Pda_Total_Station
{
    class ToolClass
    {
        public static string configpath = @"Program Files\TpsAutoSurvey_PDA_Version_\config.xml";
        public static string[] Split(string str, string splitstr)
        {
            return str.Split(Convert.ToChar(splitstr));
        }

        //pda数据库很奇怪,日期跟台式机格式不同.
        public static string datetime_str(DateTime time)
        {
            string str = time.ToString("M-d-yyyy HH:mm:ss");
            return str;
        }

    

        public static double GetZeroAngle(double txt)
        {
            double angle = txt;
            double d = Math.Floor(angle);
            double m = Math.Floor((angle - d) * 100);
            double s = (angle * 100 - Math.Floor(angle * 100)) * 100;
            angle = (d + m / 60 + s / 3600) * 3.1415926 / 180;
            return angle;
        }

        public static void Log(string txt)
        {
            string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString())
        + "\\log.txt";
            using (StreamWriter sw = new StreamWriter(path, true))
            {
                txt = DateTime.Now.ToString("yyyy-M-d HH:mm:ss") + "   " + txt;
                sw.WriteLine(txt);
                sw.Close();
            }
        }
        public static string GetProfileString(string key)
        {
            return GetProfileString(configpath, key);
        }
        public static string GetProfileString(string path,  string key)
        {
            using (XmlReader reader = XmlReader.Create(path))
            {
                try
                {
                    // 解析文件，并显示每一个节点
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                reader.MoveToAttribute("name");
                                if (reader.Value == key)
                                {
                                    reader.MoveToAttribute("value");
                                    return reader.Value;
                                }
                                else
                                {
                                    break;
                                }
                            default:
                                break;
                        }
                    }
                }
                catch (XmlException ex)
                {
                    Log(ex.Message);
                    return "";
                }
                reader.Close();
            }
            return "";
        }


        public static void SetProfileString(string path, string key, string newvalue)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            try
            {
                XmlNode root = xmlDoc.SelectSingleNode("Config");
                foreach (XmlNode n in root.ChildNodes)
                {
                    if (n.Attributes["name"].Value == key)
                    {
                        n.Attributes["value"].Value = newvalue;
                        xmlDoc.Save(path);
                        return;
                    }
                }
                XmlElement xe1 = xmlDoc.CreateElement("item");//创建一个<book>节点   
                xe1.SetAttribute("name", key);//设置该节点genre属性   
                xe1.SetAttribute("value", newvalue);//设置该节点ISBN属性   
                root.AppendChild(xe1);
                
           
            }
            catch (XmlException ex)
            {
                Log(ex.Message);
                return;
            }
        }
        public static void SetProfileString(string key, string newvalue)
        {
            ToolClass.SetProfileString(ToolClass.configpath, key, newvalue);
        }
    }
}
